package com.example.aircraft.base;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;

import com.example.aircraft.MyApplication;
import com.example.aircraft.R;

/**
 * Music Play Service Base
 * 生命周期比较长
 * 主要负责背景音乐的播放
 * @author lzf
 */

public class OpenMusicService extends Service {
    protected MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.bgm_1002);

    }
    @Override
    public int onStartCommand (Intent intent,int flag, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent,flag,startId);
        if(!player.isPlaying()){
            // 开始播放
            player.start();
            // 允许循环播放
            player.setLooping(true);
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(player.isPlaying()){
        player.stop();
        }
        player.release();

    }
}
